<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FileUpload</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        var file;
        var directory = [];
        function upload(input) {
            file=input.files[0];
        }
        function uploads(input) {
            directory=input.files;
        }
        function check() {
            if(file!=null)
                alert(file.name);
            else {
                for (var i = 0; i < directory.length; i++)
                    alert(directory[i].webkitRelativePath);
            }
        }
        function uploadFile() {
            var fd=new FormData();
            fd.append("file",file);
            $.ajax({
                url: "../files/uploadFile",
                type: "post",
                data: fd,
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
                    alert(data);
                },
                error: function () {
                    alert("操作失败")
                }
            })
        }
        function uploadDirectory() {
            var fd = new FormData();
            for (var i = 0; i < directory.length; i++) {
                fd.append("files", directory[i]);
                fd.append("paths",directory[i].webkitRelativePath);
            }
            $.ajax({
                url: "../files/uploadFiles",
                type: "post",
                data: fd,
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
                    if(data=="success")
                        alert("成功")
                    else
                        alert(data);
                },
                error: function () {
                    alert("操作失败")
                }
            })
        }

    </script>
</head>
<body>
<input type="file" onchange="uploads(this)" webkitdirectory >
<button id="upload-btn" type="button" onclick="check()">upload</button>
<button id="test" type="button" onclick="uploadDirectory()">UploadDirectory</button>
<br>
<input id="file" type="file" onchange="upload(this)">
<button id="upload-btn2" type="button" onclick="check()">upload</button>
<button id="upload" type="button" onclick="uploadFile()">UploadFile</button>
</body>
</html>